package com.pai4j.log.controller;

import com.pai4j.common.bean.PAIPageResponseBeanUtil;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.log.service.ILogService;
import com.pai4j.log.vo.LogQueryRequest;
import com.pai4j.log.vo.LogStatisticsRequest;
import com.pai4j.log.vo.LogStatisticsVO;
import com.pai4j.log.vo.OperationLogVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * 日志管理控制器
 *
 * @author: CYM-pai
 * @date: 2025/07/29 16:10
 **/
@Slf4j
@RestController
@RequestMapping("/log-service/admin")
public class LogController {

    @Resource
    private ILogService logService;

    /**
     * 查询操作日志列表
     *
     * @param request 查询请求
     * @return 分页日志数据
     */
    @GetMapping("/operation/list")
    public PAIPageResponseBeanUtil<List<OperationLogVO>> getOperationLogs(LogQueryRequest request) {
        try {
            return logService.queryOperationLogs(request);
        } catch (Exception e) {
            log.error("查询操作日志失败", e);
            return PAIPageResponseBeanUtil.error();
        }
    }

    /**
     * 获取日志详情
     *
     * @param logId 日志ID
     * @return 日志详情
     */
    @GetMapping("/operation/detail/{logId}")
    public PAIResponseBean<OperationLogVO> getLogDetail(@PathVariable String logId) {
        try {
            OperationLogVO logDetail = logService.getLogDetail(logId);
            if (logDetail != null) {
                return PAIResponseBeanUtil.success(logDetail);
            } else {
                return PAIResponseBeanUtil.error(404, "日志不存在");
            }
        } catch (Exception e) {
            log.error("获取日志详情失败", e);
            return PAIResponseBeanUtil.error(500, "获取失败");
        }
    }

    /**
     * 根据链路追踪ID查询日志
     *
     * @param traceId 链路追踪ID
     * @return 日志列表
     */
    @GetMapping("/trace/{traceId}")
    public PAIResponseBean<List<OperationLogVO>> getTraceLog(@PathVariable String traceId) {
        try {
            List<OperationLogVO> logs = logService.getLogsByTraceId(traceId);
            return PAIResponseBeanUtil.success(logs);
        } catch (Exception e) {
            log.error("根据链路追踪ID查询日志失败", e);
            return PAIResponseBeanUtil.error(500, "查询失败");
        }
    }

    /**
     * 获取日志统计信息
     *
     * @param request 统计请求
     * @return 统计结果
     */
    @GetMapping("/statistics")
    public PAIResponseBean<List<LogStatisticsVO>> getStatistics(LogStatisticsRequest request) {
        try {
            List<LogStatisticsVO> statistics = logService.getLogStatistics(request);
            return PAIResponseBeanUtil.success(statistics);
        } catch (Exception e) {
            log.error("获取日志统计信息失败", e);
            return PAIResponseBeanUtil.error(500, "获取失败");
        }
    }

    /**
     * 清理过期日志
     *
     * @param days 保留天数
     * @return 清理结果
     */
    @DeleteMapping("/clean")
    public PAIResponseBean<Long> cleanExpiredLogs(@RequestParam(defaultValue = "30") int days) {
        try {
            Long cleanCount = logService.cleanExpiredLogs(days);
            return PAIResponseBeanUtil.success(cleanCount);
        } catch (Exception e) {
            log.error("清理过期日志失败", e);
            return PAIResponseBeanUtil.error(500, "清理失败");
        }
    }
}
